package logic

import (
	"context"
	"errors"
	"house_srv/cache"
	"house_srv/mysql"

	"house_srv/house"
	"house_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateAvatarImagesLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateAvatarImagesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateAvatarImagesLogic {
	return &UpdateAvatarImagesLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateAvatarImagesLogic) UpdateAvatarImages(in *house.UpdateAvatarImagesRequest) (*house.UpdateAvatarImagesResponse, error) {
	// todo: add your logic here and delete this line
	user := mysql.User{}
	err := user.UserGetMobile(in.Mobile)
	if err != nil {
		logx.Info("手机号未注册,请重新注册", err)
		return nil, err
	}
	user.ID = uint(in.UserId)
	user.Avatar = in.Avatar
	err = user.UpdateUserAvatar(in.UserId, in.Avatar)
	if err != nil {
		logx.Info("修改图片失败", err)
		return nil, err
	}
	get := cache.Rdb.Get(l.ctx, "sendSmsupdate"+in.Mobile)
	if get.Val() != in.SendSms {
		logx.Info("获取短信验证码信息失败", err)
		return nil, errors.New("获取短信验证码信息失败")
	}
	return &house.UpdateAvatarImagesResponse{
		UserId: int64(user.ID),
	}, nil

}
